/*
 * FlereKommunikationTestPC.java
 *
 * Created on __DATE__, __TIME__
 */

package FlereKommunikationsTest;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.pc.comm.NXTConnector;

/**
 *
 * @author  __USER__
 */
public class FlereKommunikationTestPC extends javax.swing.JFrame {

	/** Creates new form FlereKommunikationTestPC */
	public FlereKommunikationTestPC() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		ButtonHej = new javax.swing.JButton();
		BeskedButton = new javax.swing.JButton();
		farvelButton = new javax.swing.JButton();
		device1Text = new javax.swing.JTextField();
		device2Text = new javax.swing.JTextField();
		device3Text = new javax.swing.JTextField();
		status1Text = new javax.swing.JTextField();
		status2Text = new javax.swing.JTextField();
		status3Text = new javax.swing.JTextField();
		connect1Button = new javax.swing.JButton();
		connect2Button = new javax.swing.JButton();
		connect3Button = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		ButtonHej.setText("Hej");
		ButtonHej.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonHejActionPerformed(evt);
			}
		});

		BeskedButton.setText("Besked");
		BeskedButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				BeskedButtonActionPerformed(evt);
			}
		});

		farvelButton.setText("Farvel");
		farvelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				farvelButtonActionPerformed(evt);
			}
		});

		device1Text.setText("Device 1");

		device2Text.setText("Device 2");

		device3Text.setText("Device 3");

		status1Text.setEditable(false);
		status1Text.setText("disconected");

		status2Text.setEditable(false);
		status2Text.setText("disconected");

		status3Text.setEditable(false);
		status3Text.setText("disconected");

		connect1Button.setText("Connect");
		connect1Button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				connect1ButtonActionPerformed(evt);
			}
		});

		connect2Button.setText("Connect");
		connect2Button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				connect2ButtonActionPerformed(evt);
			}
		});

		connect3Button.setText("Connect");
		connect3Button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				connect3ButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														BeskedButton,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														ButtonHej,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														farvelButton,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(17, 17,
																		17)
																.addComponent(
																		device1Text,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(18, 18,
																		18)
																.addComponent(
																		connect1Button,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		121,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										device2Text,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addGap(18,
																										18,
																										18)
																								.addComponent(
																										connect2Button,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										120,
																										Short.MAX_VALUE))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										device3Text,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addGap(18,
																										18,
																										18)
																								.addComponent(
																										connect3Button,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										120,
																										Short.MAX_VALUE)))))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										12,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(status3Text)
												.addComponent(status2Text)
												.addComponent(
														status1Text,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														192, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														device1Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(ButtonHej)
												.addComponent(
														status1Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(connect1Button))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(BeskedButton)
												.addComponent(
														device2Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														status2Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(connect2Button))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														device3Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(farvelButton)
												.addComponent(
														status3Text,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(connect3Button))));

		pack();
	}// </editor-fold>
		//GEN-END:initComponents

	private void farvelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		try {

			char[] message = { 'F', 'a', 'r', 'v', 'e', 'l' };

			if (dos1 != null) {
				dos1.writeInt(message.length);
				dos1.flush();
			}
			if (dos2 != null) {
				dos2.writeInt(message.length);
				dos2.flush();
			}
			if (dos3 != null) {
				dos3.writeInt(message.length);
				dos3.flush();
			}

			for (char c : message) {
				if (dos1 != null) {
					dos1.writeChar(c);
					dos1.flush();
				}
				if (dos2 != null) {
					dos2.writeChar(c);
					dos2.flush();
				}
				if (dos3 != null) {
					dos3.writeChar(c);
					dos3.flush();
				}
			}

			if (dis1 != null) {
				status1Text.setText("Recived " + dis1.readInt() + " messages");
			}
			if (dis2 != null) {
				status2Text.setText("Recived " + dis2.readInt() + " messages");
			}
			if (dis3 != null) {
				status3Text.setText("Recived " + dis3.readInt() + " messages");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void BeskedButtonActionPerformed(java.awt.event.ActionEvent evt) {
		try {

			char[] message = { 'B', 'e', 'S', 'k', 'e', 'd' };

			if (dos1 != null) {
				dos1.writeInt(message.length);
				dos1.flush();
			}
			if (dos2 != null) {
				dos2.writeInt(message.length);
				dos2.flush();
			}
			if (dos3 != null) {
				dos3.writeInt(message.length);
				dos3.flush();
			}

			for (char c : message) {
				if (dos1 != null) {
					dos1.writeChar(c);
					dos1.flush();
				}
				if (dos2 != null) {
					dos2.writeChar(c);
					dos2.flush();
				}
				if (dos3 != null) {
					dos3.writeChar(c);
					dos3.flush();
				}
			}

			if (dis1 != null) {
				status1Text.setText("Recived " + dis1.readInt() + " messages");
			}
			if (dis2 != null) {
				status2Text.setText("Recived " + dis2.readInt() + " messages");
			}
			if (dis3 != null) {
				status3Text.setText("Recived " + dis3.readInt() + " messages");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void ButtonHejActionPerformed(java.awt.event.ActionEvent evt) {
		try {

			char[] message = { 'H', 'e', 'j' };

			if (dos1 != null) {
				dos1.writeInt(message.length);
				dos1.flush();
			}
			if (dos2 != null) {
				dos2.writeInt(message.length);
				dos2.flush();
			}
			if (dos3 != null) {
				dos3.writeInt(message.length);
				dos3.flush();
			}

			for (char c : message) {
				if (dos1 != null) {
					dos1.writeChar(c);
					dos1.flush();
				}
				if (dos2 != null) {
					dos2.writeChar(c);
					dos2.flush();
				}
				if (dos3 != null) {
					dos3.writeChar(c);
					dos3.flush();
				}
			}

			if (dis1 != null) {
				status1Text.setText("Recived " + dis1.readInt() + " messages");
			}
			if (dis2 != null) {
				status2Text.setText("Recived " + dis2.readInt() + " messages");
			}
			if (dis3 != null) {
				status3Text.setText("Recived " + dis3.readInt() + " messages");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void connect3ButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// Connect to a NXT over Bluetooth
		if (dos3 == null) {
			boolean connected = conn3.connectTo("btspp://"
					+ device3Text.getText());
			if (!connected) {
				status3Text.setText("No Connection");
			} else {
				//create streams
				dos3 = conn3.getDataOut();
				dis3 = conn3.getDataIn();
				status3Text.setText("Ready");
				connect3Button.setText("Disconnect");
			}
		} else {
			try {
				dos3.writeInt(-1);
				dos3.flush();
				dos3.close();
				dos3 = null;
				dis3.close();
				dis3 = null;
				conn3.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connect2Button.setText("Connect");

		}
	}

	private void connect2ButtonActionPerformed(java.awt.event.ActionEvent evt) {
		if (dos2 == null) {
			// Connect to a NXT over Bluetooth
			boolean connected = conn2.connectTo("btspp://"
					+ device2Text.getText());
			if (!connected) {
				status2Text.setText("No Connection");
			} else {
				//create streams
				dos2 = conn2.getDataOut();
				dis2 = conn2.getDataIn();
				connect2Button.setText("Disconnect");
			}
		} else {
			try {
				dos2.writeInt(-1);
				dos2.flush();
				dos2.close();
				dos2 = null;
				dis2.close();
				dis2 = null;
				conn2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connect2Button.setText("Connect");

		}
	}

	private void connect1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// Connect to a NXT over Bluetooth
		if (dos1 == null) {
			boolean connected = conn1.connectTo("btspp://"
					+ device1Text.getText());
			if (!connected) {
				status1Text.setText("No Connection");
			} else {
				//create streams
				dos1 = conn1.getDataOut();
				dis1 = conn1.getDataIn();
				connect1Button.setText("Disconnect");
			}
		} else {
			try {
				dos1.writeInt(-1);
				dos1.flush();
				dos1.close();
				dos1 = null;
				dis1.close();
				dis1 = null;
				conn1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connect1Button.setText("Connect");

		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new FlereKommunikationTestPC().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton BeskedButton;
	private javax.swing.JButton ButtonHej;
	private javax.swing.JButton connect1Button;
	private javax.swing.JButton connect2Button;
	private javax.swing.JButton connect3Button;
	private javax.swing.JTextField device1Text;
	private javax.swing.JTextField device2Text;
	private javax.swing.JTextField device3Text;
	private javax.swing.JButton farvelButton;
	private javax.swing.JTextField status1Text;
	private javax.swing.JTextField status2Text;
	private javax.swing.JTextField status3Text;
	// End of variables declaration//GEN-END:variables

	//kommunikation
	private static DataOutputStream dos1;
	private static DataInputStream dis1;
	public NXTConnector conn1 = new NXTConnector();
	private static DataOutputStream dos2;
	private static DataInputStream dis2;
	public NXTConnector conn2 = new NXTConnector();
	private static DataOutputStream dos3;
	private static DataInputStream dis3;
	public NXTConnector conn3 = new NXTConnector();

}